import QtQuick 1.1
import com.nokia.symbian 1.1
import "js/storage.js" as Storage
import "js/gnews.js" as Gnews

PageStackWindow {
    id: appWindow

    property variant settings:  {}
    property string currentNed: "us"
    property string currentTopic: "h"
    property string currentTopicColor: "#6B90DA"
    property variant topicsOrder: ["h","w","n","b","t","p","e","s","m","ir","po"]
    property variant topicsHidden: []
    property bool loadImages: true
    property bool gMobilizer: false
    property bool settingsComplete: false
    property bool lightTheme: true
    property bool deprecationWarning: true
    Component.onCompleted: onStartup()

    platformInverted : lightTheme;


    function onStartup() {
        var defaults = {
            defaultNed:currentNed,
            defaultTopic: currentTopic,
            loadImages: loadImages,
            gMobilizer: gMobilizer,
            topicsOrder: JSON.stringify(topicsOrder),
            topicsHidden: JSON.stringify(topicsHidden),
            lightTheme: platformInverted,
            deprecationWarning: true
        }

        Storage.loadSettings(defaults, settingsLoaded);
        pageStack.push(mainPage);
    }

    QueryDialog{
        id: informationDialog

        platformInverted: appWindow.platformInverted

        titleText: "Deprecation Warning"
        message: "Note that the Google NewsSearch Service used by this app has been deprecated. It could stop working any time Google chooses not to support it anymore."

        acceptButtonText: "Ok"
    }


    function settingsLoaded(dbSettings) {
        settings = dbSettings
        currentNed = settings.defaultNed
        currentTopic = settings.defaultTopic
        currentTopicColor = Gnews.getTopicColor(currentTopic)
        loadImages = settings.loadImages
        gMobilizer = settings.gMobilizer
        topicsOrder = JSON.parse(settings.topicsOrder)
        topicsHidden = JSON.parse(settings.topicsHidden)
        lightTheme = settings.lightTheme
        deprecationWarning = settings.deprecationWarning
        settingsComplete = true

        mainPage.start()
    }

    function saveSettingValue(key, value) {
        console.log('save setting: '+key+' = '+value)
        Storage.insertSetting(key, value);
    }

    function isHiddenTopic(topic) {
        var max = appWindow.topicsHidden.length;
        for(var j=0; max > j;j++) {
            if(topic == appWindow.topicsHidden[j]) {
                return true;
            }
        }
        return false;
    }

    function getManagedTopics(unOrderedTopics) {
        var topics = [];
        var max = appWindow.topicsOrder.length;
        for(var i=0;i< max;i++) {
            var data = unOrderedTopics[appWindow.topicsOrder[i]];
            data.visibility = true;
            if(isHiddenTopic(appWindow.topicsOrder[i])) {
                data.visibility = false;
            }
            topics.push(data);
        }
        return topics;
    }

    function changeDefaultNedLabel(newLabel) {
        defaultNedButton.text = '<span style="color:grey;font-size:small">Default edition </span>  '+newLabel
    }

    function changeDefaultTopicLabel(newLabel) {
        defaultTopicButton.text = '<span style="color:grey;font-size:small">Default topic </span>  '+newLabel
    }

    function startSpinner() {
        loadingSpinner.running = true
        loadingSpinner.visible = true
        refreshIcon.visible = false
    }

    function stopSpinner() {
        loadingSpinner.visible = false
        loadingSpinner.running = false
        refreshIcon.visible = true
    }

    PageStack {
        id: pageStack
        toolBar: toolBar
    }

    MainPage {
        id: mainPage
        anchors.top: statusBar.bottom
    }

    StatusBar {
        id: statusBar
    }

    BusyIndicator {
        id: loadingSpinner
        running: true
        visible: false
        anchors.centerIn: parent
        width: 50
        height: 50
    }

    ToolBar {
        id: toolBar
        anchors.bottom: parent.bottom // appWindow.bottom
        platformInverted: appWindow.platformInverted

        tools: ToolBarLayout {
            id: commonTools
            ToolButton {
                flat: true
                iconSource: "toolbar-back"
                platformInverted: appWindow.platformInverted
                onClicked: pageStack.depth == 1 ? Qt.quit() : pageStack.pop();
            }

            ToolButton {
                 id: searchIcon
                 iconSource: "toolbar-search";
                 visible: true
                 platformInverted: appWindow.platformInverted
                 // anchors.right: refreshIcon.left
                 onClicked: {
                     searchPage.startup();
                     pageStack.push(searchPage);
                 }
            }

            ToolButton {
                 id: refreshIcon
                 iconSource: "toolbar-refresh";
                 visible: true
                 platformInverted: appWindow.platformInverted
                 // anchors.right: settingsIcon.left
                 onClicked: (refreshIcon.visible == true) ? mainPage.doRefresh() : false
            }
            ToolButton {
                 id: settingsIcon
                 iconSource: "toolbar-menu";
                 platformInverted: appWindow.platformInverted
                 // anchors.right: parent===undefined ? undefined : parent.right
                 onClicked: (myMenu.status == DialogStatus.Closed) ? myMenu.open() : myMenu.close()
            }
        }
    }

    EditionSelectionDialog {
        id:editionSelectionDialog
    }

    DefaultEditionDialog {
        id:defaultEditionDialog
    }

    DefaultTopicDialog {
        id:defaultTopicDialog
    }

    TopicsManagerDialog {
        id:topicManager
    }

    SearchPage {
        id:searchPage
    }

    BrowserPage {
        id:browserPage
    }

    Dialog {
       id: aboutDialog
       onClickedOutside: close()

       title:Text {
           text: "News for Symbian"
           font.pointSize: 14
           color: "white"
           anchors.centerIn: parent

       }

       content:Item {
            id: name
            width: parent.width
            height: 300
            Text {
                font.pixelSize:12
                color: "white"
                anchors.centerIn: parent
                text: parent.getAboutMsg()
                textFormat: Text.RichText
                wrapMode: Text.WordWrap
                onLinkActivated: Qt.openUrlExternally(link)
            }
            function getAboutMsg() {
                 var msg = 'V1.3.0 - Ported to Symbian by Oliver Fink<br/>'
                 msg += 'Content: <a href="http://code.google.com/apis/newssearch/">Google Newsearch API</a><br/>'
                 msg += 'Sources: <a href="http://projects.developer.nokia.com/news">http://projects.developer.nokia.com/news</a><br/>'
                 msg += 'License: GNU General Public License (GPL) Vers.2<br/>';
                 msg += 'Icon from The Noun Project <a href="http://thenounproject.com">http://thenounproject.com</a><br/>'
                 msg += '<br/>Based on NewsG for MeeGo<br/>&#169; 2011, Martin Borho <a href="mailto:martin@borho.net">martin@borho.net</a><br/>';
                 msg += 'Original Source: <a href="http://github.com/mborho/NewsG-Meego">http://github.com/mborho/NewsG-Meego</a>';

                 msg += '</p>';
                 return msg
            }
        }
    }

    Menu {
        id: myMenu
        visualParent: pageStack
        platformInverted: appWindow.platformInverted
        MenuLayout {
            MenuItem { text: "Select edition"; onClicked: editionSelectionDialog.openDialog(); platformInverted: appWindow.platformInverted;  }
            MenuItem {
                text: "Manage topics"
                platformInverted: appWindow.platformInverted;
                onClicked: {
                    topicManager.loadModel();
                    pageStack.push(topicManager);
                }
            }
            MenuItem {
                text: 'Load images'
                platformInverted: appWindow.platformInverted;
                Switch {
                    id: imagesSwitch
                    platformInverted: appWindow.platformInverted;
                    checked: appWindow.loadImages
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: parent.right
                    anchors.rightMargin: 30
                    onCheckedChanged: (settingsComplete === true) ? loadImagesChanged() : false

                    function loadImagesChanged() {
                        appWindow.saveSettingValue('loadImages', checked)
                        appWindow.loadImages = checked
                    }
                 }
            }
            MenuItem {
                text: 'Light theme'
                platformInverted: appWindow.platformInverted;
                Switch {
                    id: themeSwitch
                    platformInverted: appWindow.platformInverted;
                    checked: appWindow.platformInverted
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: parent.right
                    anchors.rightMargin: 30
                    onCheckedChanged: (settingsComplete === true) ? themeSwitchChanged() : false

                    function themeSwitchChanged() {
                        appWindow.saveSettingValue('lightTheme', checked)
                        appWindow.platformInverted = checked
                        mainPage.doRefresh()
                    }
                 }
            }
            MenuItem {
                text: 'Open links in Readability'
                platformInverted: appWindow.platformInverted;
                Switch {
                    id: gMobilizerSwitch
                    platformInverted: appWindow.platformInverted;
                    checked: appWindow.gMobilizer
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: parent.right
                    anchors.rightMargin: 30
                    onCheckedChanged: (settingsComplete === true) ? gMobilizerChanged() : false;

                    function gMobilizerChanged() {
                        appWindow.saveSettingValue('gMobilizer',checked)
                        appWindow.gMobilizer = checked
                    }
                }
            }
            MenuItem {
                id:defaultTopicButton
                platformInverted: appWindow.platformInverted;
                text: '<span style="color:grey;font-size:small">Default topic </span>  '+Gnews.getConfTopicLabel(appWindow.settings.defaultTopic);
                onClicked: defaultTopicDialog.openDialog()
            }
            MenuItem {
                id:defaultNedButton
                platformInverted: appWindow.platformInverted;
                text: '<span style="color:grey;font-size:small">Default edition </span>  '+Gnews.getEditionLabel(appWindow.settings.defaultNed)
                onClicked: defaultEditionDialog.openDialog()
            }

            MenuItem {
                id:aboutButton
                platformInverted: appWindow.platformInverted;
                text: 'About'
                onClicked: aboutDialog.open()
            }
        }
    }
}

